package com.luofan.common.util;

/**
 * ThreadLocal工具类
 * jwt过滤器中已存入用户id,role,sectionId，后续方法可直接使用类中的静态方法获取数据
 */
public class UserContext {

    private static final ThreadLocal<Long> currentUserId = new ThreadLocal<>();  //用户id
    private static final ThreadLocal<Byte> currentRole = new ThreadLocal<>();    //职权标识
    private static final ThreadLocal<Long> currentSectionId = new ThreadLocal<>(); //所属教研室id

    //将id role sectionId存入ThreadLocal中
    public static void setCurrentUser(Long userId, Byte role, Long sectionId) {
        currentUserId.set(userId);
        currentRole.set(role);
        currentSectionId.set(sectionId);
    }

    //获取ThreadLocal中的id
    public static Long getCurrentUserId() {
        return currentUserId.get();
    }

    //获取ThreadLocal中的role
    public static Byte getCurrentRole() {
        return currentRole.get();
    }

    //获取ThreadLocal中的propertyId
    public static Long getCurrentSectionId() {
        return currentSectionId.get();
    }

    //清除数据，避免内存泄露
    public static void clear() {
        currentUserId.remove();
        currentRole.remove();
        currentSectionId.remove();
    }
}
